<?php

require_once PATH_CLASS.'base.class.php';
require_once PATH_CLASS.'theme.class.php';

class smartyFilter extends base {
  
  static $smarty = null;
  static $type  = '';
  
  public static function call(&$output,$name,$sub=''){
    $ret = '';
    $class = __CLASS__;
    $methods = get_class_methods($class);
    $name = strtolower($name);
    $methode = $name."Filter";
    if(in_array($methode,$methods)){
      $ret = call_user_func_array(array($class,$methode),array($output,$sub));
    }
    
    return $ret;
  }
  
  public static function runFilter(&$output, Smarty_Internal_Template &$smarty, $type){
    self::$smarty = $smarty;
    self::$type   = $type;
    $ret = $output;
    if(preg_match_all("/%%FILTER:LOLITA:((\w*)(?::(\w*))?)%%/", $ret,$res)==true){
    
      foreach ($res[0] as $key=>$val){
        $full     = $res[0][$key];
        $root     = $res[2][$key];
        $sub      = $res[3][$key];
        $replace  = self::call($output, $root, $sub);
        if(is_null($replace)){
          $replace='';
        }
        $ret = preg_replace("/".$full."/", $replace, $ret);
      }
    }
    
    
    return $ret;
  }
  
  public static function bodyFilter(&$output,$sub){
    
    $ret=array();
    
    switch(strtoupper($sub)){
      case 'CLASS' :
        $ret[]  = "theme-".theme::getThemeName();
        break;
    }
    return join(' ', $ret);
  }
  
}

